var app = getApp();
var $ = require("../../utils/utils.js")
const config = require('../../config');
var WxParse = require('../../wxParse/wxParse.js');

Page({

    /**
     * 页面的初始数据
     */
    data: {
        bookDetail:{},
        bookDetailHtml: null,
        author: "作者",
        isbn: '',
        book_name: "绘本名",
        postImg: "../../img/defaultPic.png",
        press: "",
        publish_date: "",
        shelf_code: "",
        describe: "",
        bookurls: [],
        readerUser: [],
        readerCount: 0,
        isGetData:false,
        isLogin:false
    },

    
/********************LifeCycle**************/
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        if (options.book_isbn) {
            let params = {
                isbn: options.book_isbn,
            };
            this.getBorrowedUser(params)
            this.getBookInfo(params)
        }

        wx.setStorage({
            key: 'book_isbn',
            data: options.book_isbn
        });
    },
    onShow:function(){
        let login = wx.getStorageSync("isLogin")
        this.setData({
            isLogin: login
        });
    },


/********************Methods**************/
    // 获取书籍详情
    getBookInfo: function (params) {

        let infoUrl = $.getUrl((config.getBookDetail), params)
        $.get(infoUrl).then(data => {
            this.setData({
                bookDetail: data.detail,
                postImg: data.detail.bookbanners[0].bookurl,
                distance: data.detail.distance,
                book_name: data.detail.bookname,
                isbn: data.detail.isbn,
                press: data.detail.press,
                publish_date: data.detail.publish_date,
                shelf_code: data.detail.shelf_code,
                author: data.detail.author,
                describe: data.detail.describe,
                bookurls: data.detail.bookurls,
                isGetData:true
            })

            var describeDetail = data.detail.describe
            WxParse.wxParse('describeDetail', 'html', describeDetail, this, 5);

            wx.setNavigationBarTitle({
                title: data.detail.bookname
            });
        })
    },

    // 获取已借阅用户
    getBorrowedUser: function (params) {
        let infoUrl = $.getUrl(config.queryBorrowedUser, params)
        $.get(infoUrl).then(data => {
            let arr = Array.from(data.items).filter((item, index) => {
                if (index < 5) {
                    return item;
                }
            })

            this.setData({
                readerCount: data.items.length,
                readerUser: arr
            })
        })

    },

    toScanCode: function (e) {
        let that = this
        wx.setStorageSync("bookDetail_isbn", that.data.isbn);
        wx.setStorageSync("bookDetailData", JSON.stringify(that.data.bookDetail))
        
        if (!wx.getStorageSync("isLogin")) {
            if (e.detail.errMsg == 'getPhoneNumber:ok') {
                app.aesPhoneNumber(e.detail.iv, e.detail.encryptedData).then(data => {
                    app.scanCode();
                })
            } else if (e.type == "getphonenumber"){
                app.scanCode(); 
            }
        }else{
            app.scanCode(); 
        }

        
          

    },



    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function (res) {
        var that = this;
        var path = '/page/test_webview/test_webview?book_isbn=' + that.data.isbn;
        wx.getStorage({
            key: 'book_isbn',
            success: function (res) {
                return {
                    title: '我的邦',
                    path: '/page/test_webview/test_webview?book_isbn=' + res.data,
                    success: function (share) {
                        // 转发成功
                        console.log("分享", path);
                    },
                    fail: function (res) {
                        // 转发失败
                    }
                }
            },
        })

    }
})